package org.dtrd.modules.achievement.entity.bean;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.dtrd.config.system.Constant;
import org.dtrd.modules.achievement.entity.po.DtrdEntRdSigninPlan;

import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Data
@ApiModel(value = "打卡计划")
public class PatientSigninPlan extends BaseSigninPlan{

    @ApiModelProperty(value = "备注", notes = "仅在打卡计划暂停时有")
    private String remark;
    @ApiModelProperty(value = "操作人")
    private String editor;


    @ApiModelProperty(value = "打卡计划-JSON串-周一计划")
    private List<JSONObject> signInJsonMonday;

    @ApiModelProperty(value = "打卡计划-JSON串-周二计划")
    private List<JSONObject> signInJsonTuesday;

    @ApiModelProperty(value = "打卡计划-JSON串-周三计划")
    private List<JSONObject> signInJsonWednesday;

    @ApiModelProperty(value = "打卡计划-JSON串-周四计划")
    private List<JSONObject> signInJsonThursday;

    @ApiModelProperty(value = "打卡计划-JSON串-周五计划")
    private List<JSONObject> signInJsonFriday;

    @ApiModelProperty(value = "打卡计划-JSON串-周六计划")
    private List<JSONObject> signInJsonSaturday;

    @ApiModelProperty(value = "打卡计划-JSON串-周日计划")
    private List<JSONObject> signInJsonSunday;

    public PatientSigninPlan fromPO(DtrdEntRdSigninPlan po) {
        setDataId(po.getDataId());
        setPatientId(po.getPatientId());
        setSignInTemplateId(po.getSignInTemplateId());
        setSignInStartdate(po.getSignInStartdate());
        setSignInEnddate(po.getSignInEnddate());
        setSignInStatus(po.getSignInStatus());
        if (po.getSignInStatus() != null) {
            setSignInStatusText(Constant.SignInPlanUsageStatus.valueOf(po.getSignInStatus()).getName());
        }
        remark = po.getRemark();
        signInJsonMonday = addProperty(po.getSignInJsonMonday());
        signInJsonTuesday = addProperty(po.getSignInJsonTuesday());
        signInJsonWednesday = addProperty(po.getSignInJsonWednesday());
        signInJsonThursday = addProperty(po.getSignInJsonThursday());
        signInJsonFriday = addProperty(po.getSignInJsonFriday());
        signInJsonSaturday = addProperty(po.getSignInJsonSaturday());
        signInJsonSunday = addProperty(po.getSignInJsonSunday());
        setSignInJson(po.getSignInJsonWorkingday());
        return this;
    }

    private List<JSONObject> addProperty(String signinTemplate) {
        List<JSONObject> jsonObjects = JSONObject.parseArray(signinTemplate, JSONObject.class);
        jsonObjects.forEach(jsonObject -> {
            Integer recordType = jsonObject.getInteger("recordType");
            Integer subType = jsonObject.getInteger("subType");
            jsonObject.put("item_id", recordType + "+" + subType);
            StringJoiner joiner = new StringJoiner("");
            if (subType != null) {
                if (recordType == Constant.RecordType.SUGAR.getType()) {
                    joiner.add(Constant.SugarRecordType.valueOf(subType).getName());
                }
                if (recordType == Constant.RecordType.DIET.getType()) {
                    joiner.add(Constant.FoodRecordType.valueOf(subType).getName());
                }
                if (recordType == Constant.RecordType.MEDICAL.getType()) {
                    joiner.add(Constant.MedicalRecordType.valueOf(subType).getName());
                }
                if (recordType == Constant.RecordType.INSULIN.getType()) {
                    joiner.add(Constant.InsulinRecordType.valueOf(subType).getName());
                }
                if (recordType == Constant.RecordType.BLOOD_PRESSURE.getType()) {
                    joiner.add(Constant.BloodPressureRecordType.valueOf(subType).getName());
                }
                if (recordType == Constant.RecordType.NUTRIENT.getType()) {
                    joiner.add(Constant.NutrientRecordType.valueOf(subType).getName());
                }
            }
            if (recordType != null) {
                joiner.add(Constant.RecordType.valueOf(recordType).getName());
            }
            jsonObject.put("item", joiner.toString());
        });
        return jsonObjects;
    }

}